package com.gxc.datacenter.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.datacore.metadata.domain.Database;
import com.gxc.datacore.metadata.service.DatabaseService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

@Controller("datacenter/databaseController")
public class DatabaseController {

	@Autowired
	private DatabaseService databaseService;

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("datacenter/manage/database_list.vm", null);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map params = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = databaseService.listPage(params, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Database database = databaseService.retrieve(id);
		Map model = new HashMap();
		model.put("database", database);
		return new ModelAndView("datacenter/manage/database_view.vm", model);
	}

	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("datacenter/manage/database_create.vm");
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Database database = (Database) RequestUtil.getInObject(request,
				Database.class);
		// 名字不能为空
		if (StringUtil.isTrimEmpty(database.getDb_name())) {
			result.put("status", "failed");
		} else {
			database.setId(StringUtil.getUUID());
			databaseService.create(database);
			result.put("status", "success");
			result.put("data", database);
		}
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Database database = databaseService.retrieve(id);
		Map model = new HashMap();
		model.put("database", database);
		return new ModelAndView("datacenter/manage/database_edit.vm", model);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Database database = (Database) RequestUtil.getInObject(request,
				Database.class);
		databaseService.update(database);
		result.put("status", "success");
		result.put("data", database);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = databaseService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = databaseService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

}
